    package com.chancingpack.controller;

import com.alibaba.fastjson.JSONObject;
import com.chancingpack.dal.dao.WxUserLoginDao;
import com.chancingpack.dal.model.WxBook;
import com.chancingpack.service.MyProductService;
import com.chancingpack.service.WxBookService;
import com.chancingpack.service.WxUserLoginService;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/7/21.
 */
@Controller
@RequestMapping("/myProduct")
public class MyProductController {

    @Autowired
    private MyProductService myProductService;

    @Autowired private WxBookService wxBookService;


    /**
    *遍历所有的商品
     * 并且通过商品名字模糊查询
    * */
    @RequestMapping("/productList")
    public ModelAndView returnProductView(@RequestParam (value = "prodName" , required  = false) String prodName,@RequestParam (value = "prodExter" , required  = false) String prodExter, HttpServletRequest request){
        ModelAndView view = new ModelAndView("myproduct/myProduct");
        Map<String,Object> map = new HashMap<String, Object>();
        if(prodName!=null) {
            try {
                prodName = new String(prodName.getBytes("iso-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        Map<String,Object> usermap = new HashMap<String, Object>();
        usermap.put("custId",request.getSession().getAttribute("custId"));
        List<Map<String,Object>> userList = myProductService.selectBycustId(usermap);
        map.put("prodName",prodName);
        map.put("prodExter",prodExter);
        map.put("custCode",userList.get(0).get("cust_code"));
        List<Map<String,Object>> picturesList= myProductService.selectPictures(map,request);
        for(int i=0 ;i<picturesList.size();i++){
            if(picturesList.get(i).size()>=6) {
                String str2 = "";
                String str = picturesList.get(i).get("prodName").toString();

                if (str.indexOf("'") >= 0) {
                    str2 = str.replace("'", "‘");
                    System.out.print(str2);
                    picturesList.get(i).put("prodName", str2);
                } else {
                    picturesList.get(i).put("prodName", str);
                }
            }
        }
        view.addObject( "picturesList",JSONObject.toJSONString(picturesList));
        // 此时返回给页面的是Java对象 view.addObject("aa", aa);
        return view;
    }
    /**
    * 加入购物车
    *
    * */
    @RequestMapping("/insertBook")
    @ResponseBody
    public CommonResult  insertBook(@RequestParam(value = "archId") String archId, HttpServletRequest request){
        CommonResult result = new CommonResult();
        WxBook record = new WxBook();
        record.setArchId(archId);
        record.setCustId(request.getSession().getAttribute("custId").toString());
        result = wxBookService.addBook(record);
        return result;
    }
}
